var parentJson = {};
parentJson.limit=1000;
parentJson.headField="pno,content,meetingtheme,replydate,enddate,plantype,mno,ino,completedate,finishdate,flag,jindu,replycontent,img,backuser,decisionuser,level,expecttime,plantime,description,conclusion";
parentJson.page=1;
var enddatecalendar = new lCalendar();
var replydatecalendar = new lCalendar();
var url  = location.search;
var theRequest = new Object();  
if ( url.indexOf( "?" ) != -1 ){  
var str = url.substr( 1 ); //substr()方法返回从参数值开始到结束的字符串；  
var strs = str.split( "&" ); 
var plantype=1; 
var mno;
var ino;
for ( var i = 0; i < strs.length; i++ ) {  
theRequest[ strs[ i ].split( "=" )[ 0 ] ] = ( strs[ i ].split( "=" )[ 1 ] );  
}  
}
var aa={};
aa.conditionField = "pno";
aa.conditionValue = theRequest.pno;
aa.conditionOption = "=";
aa.logicalOperator = "and";
parentJson.search=[aa];
var now=getNowFormatDate();
var _config ;
var photoUrl = [];//拍照列表
var now = getDay(0);
var theusername = sessionStorage.getItem("name");
var theuserid = sessionStorage.getItem("userId");
console.log(parentJson);
$.ajax({
	url: "../../work_plan_info/getSelectBody"
	,type:'post'
	,async: false
	,contentType: "application/json; charset=utf-8"
	,data:JSON.stringify(parentJson) 
	,dataType:'json'
	,success: function(res){
		console.log(res.data[0]);
		console.log(111111111);
		plantype=res.data[0].plantype;
		if(res.data[0].content==undefined){
			content.value="";
		}else{content.value=res.data[0].content;}
		if(res.data[0].level==undefined){
			level.value="";
		}else{level.value=res.data[0].level;}
		if(res.data[0].expecttime==undefined){
			expecttime.value="";
		}else{expecttime.value=res.data[0].expecttime;}
		if(res.data[0].flag==undefined){
			flag.value="";
		}else{
			if(res.data[0].flag==9){flag.value="已完成";}
			else if(res.data[0].flag==10){flag.value="未完成";}
			else if(res.data[0].flag==12){flag.value="未回复";}
			else if(res.data[0].flag==500){flag.value="已回复";}
		}
		if(res.data[0].plantime==undefined){
			plantime.value="";
		}else{
			plantime.value=res.data[0].plantime;
		}
		if(res.data[0].enddate==undefined){
			enddate.value="";
		}else{
			enddate.value=res.data[0].enddate;
		}
		if(res.data[0].description==undefined){
			description.value="";
		}else{
			description.value=res.data[0].description;
		}
	  if(res.data[0].jindu==undefined){
			jindu.value="";
		}else{
			jindu.value=res.data[0].jindu;
		}
		
/*				if(res.data[0].img==undefined||res.data[0].img==""){
                     console.log("null");
                }else{
					photoUrl=[];
					photoUrl.push(res.data[0].img);	
					var result=res.data[0].img.split(",");	
					for(var i=0;i<result.length;i++){												  
						$('#wig_img').append('<div class="img_box" id="img_box'+i+'">'
						+'<img alt="100x100" src="'+result[i]+'" onclick="showAllPic(\''+result[i]+'\')"/>'
						+'<img class="close_b" src="../../genius/style/mobile_page/assets/images/close.png" onclick="deletePhoto(this)">'
						+'</div>'
						);
					}
				}*/

	
	}
});
$(document).ready(function(){
	$('#name').text(sessionStorage.getItem("name"));
	$('#searchDate').text(now);
	$('#saveButton').on('click',saveInfo);
});
$('#paizhao').on('click',function(){
	openphoto();
});
/*
enddatecalendar.init({
	'trigger': '#enddate',
	'type': 'datetime',
	'maxDate': '2099-12-31 23:59:59',
	'minDate': now
});
replydatecalendar.init({
	'trigger': '#replydate',
	'type': 'datetime',
	'maxDate': '2099-12-31 23:59:59',
	'minDate': now
});
function ICalendarClick(){
	
}	*/
function saveInfo(){
	showloading();
	try {
		var imgStr = "";
		var sendData = {};
		sendData.pno=theRequest.pno;
		sendData.plantype=plantype;
		sendData.mno=mno;
		sendData.ino=ino;
		sendData.content = $('#content').val();
		sendData.enddate = $('#enddate').val();
		sendData.shouldlastdate = $('#enddate').val();
		//sendData.completedate = $('#completedate').val();  //完成时间  共用
		sendData.completedate=getNowFormatDatetime();
		sendData.flag = $('#flag').val();                  //状态      共用
		sendData.jindu = $('#jindu').val();
		sendData.replycontent = $('#replycontent').val();   //回复内容 /结果说明  共用
		sendData.img = photoUrl.join(',');                  //图片    公用
		sendData.meetingitem=$('#content').val();
		sendData.replydate= $('#replydate').val();
 		sendData.username=theusername;
		sendData.xgr=theuserid; 
   		if(sendData.flag==10){
			if(sendData.enddate==""||sendData.enddate==null){
				alert("预计完成时间不可为空!");
			}else if(sendData.replydate==""||sendData.replydate==null){
				alert("预计回复时间不可为空!");
			}
			else{
				$.ajax({
					url: "../../work_plan_info/edit"
					,type:'post'
					,dataType:'json'
					,contentType: "application/json; charset=utf-8"
					,data:JSON.stringify(sendData) 
					,success: function(res){
					if(res.status==1000){
						layer.msg(res.msg);
					}else{
						layer.msg(res.msg,{time:1000},function(){
							history.go(-1);
						});
					}
					}
				    });
			}
		}else if(sendData.flag==9){
			sendData.finishdate=getNowFormatDatetime();
				$.ajax({
					url: "../../work_plan_info/edit"
					,type:'post'
					,dataType:'json'
					,contentType: "application/json; charset=utf-8"
					,data:JSON.stringify(sendData) 
					,success: function(res){
					if(res.status==1000){
						layer.msg(res.msg);
					}else{
						layer.msg(res.msg,{time:1000},function(){
							history.go(-1);
						});
					}
					}
				    });
		}else if(sendData.flag==500){
			if(sendData.replydate==""){
				alert("预计回复时间不可为空!");
			}else{
					$.ajax({
						url: "../../work_plan_info/edit"
						,type:'post'
						,dataType:'json'
						,contentType: "application/json; charset=utf-8"
						,data:JSON.stringify(sendData) 
						,success: function(res){
						if(res.status==1000){
							layer.msg(res.msg);
						}else{
							layer.msg(res.msg,{time:1000},function(){
								history.go(-1);
							});
						}
						}
					    });
				}
		}else{
			if(sendData.replydate!=""){
				sendData.flag=500;
			}
			$.ajax({
					url: "../../work_plan_info/edit"
					,type:'post'
					,dataType:'json'
					,contentType: "application/json; charset=utf-8"
					,data:JSON.stringify(sendData) 
					,success: function(res){
					if(res.status==1000){
						layer.msg(res.msg);
					}else{
						layer.msg(res.msg,{time:1000},function(){
							history.go(-1);
						});
					}
					}
				    });
		}
					
	
	} catch (e) {
		console.log(e)
	} finally{
		hideloading();
	}
		
}	
//获取当前日期
function getNowFormatDate() {
        var date = new Date();
        var seperator1 = "-";
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        var currentdate = year + seperator1 + month + seperator1 + strDate;
        return currentdate;
    }
//获取当前日期
function getNowFormatDatetime() {
        var date = new Date();
        var seperator1 = "-";
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
    	var strDate = date.getDate();
		var hour = date.getHours();
		var minutes = date.getMinutes();
		var seconds = date.getSeconds();
    
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }	
		if (hour >= 0 && hour <= 9) {
            hour = "0" + hour;
        }
  		if (minutes >= 0 && minutes <= 9) {
            minutes = "0" + minutes;
        }
  		if (seconds >= 0 && seconds <= 9) {
            seconds = "0" + seconds;
        }
        var currentdate = year + seperator1 + month + seperator1 + strDate + ' '+hour+':'+minutes+':'+seconds;
        return currentdate;
    }
//打开钉钉拍照
function openphoto(){
	if(_config == undefined){
		$.ajax({
			type : "post",
			data:JSON.stringify({"pageUrl":location.href.replace(/\\&/g,'%26')}),
			contentType: "application/json; charset=utf-8",
			async: false,
			url : "../../dingding/getconfig",
			dataType : "json",
			success : function(suc) {
//				console.log(suc);
				_config = suc.config;
			}
		}); 
		dd.config({
			agentId : _config.agentid,
			corpId : _config.corpId,
			timeStamp : _config.timeStamp,
			nonceStr : _config.nonceStr,
			signature : _config.signature,
			jsApiList : [ 'runtime.info','biz.util.uploadImage' ]
		});
	}
	dd.ready(function(){
		dd.biz.util.uploadImage({
		    compression: false,//(是否压缩，默认为true)
		    quality: 100, // 图片压缩质量, 
		    resize: 100, // 图片缩放率
		    stickers: {   // 水印信息
		        time: getNowFormatDateTime(),
		        username: sessionStorage.getItem("name")
		    },
		    onSuccess : function(result) {
			photoUrl.push(result[0]);
			$('#paizhapoUrl .clearfix').remove()
			$('#paizhapoUrl').append('<div class="wig_img">'
					+'<div class="img_box">'
					+'<img alt="100x100" src="'+result[0]+'" onclick="showAllPic(\''+result[0]+'\')"/>'
					+'<img class="close_b" src="../../genius/style/mobile_page/assets/images/close.png" onclick="deletePhoto(this)">'
					+'</div>'
					+'</div>');
			$('#paizhapoUrl').append('<div class="clearfix"></div>');
		    },
		    onFail : function(err) {}
		});
	});
}	
//删除图片
function deletePhoto(obj){
	dd.device.notification.confirm({
	    message: "删除图片吗",
	    title: "提示",
	    buttonLabels: ['是', '否'],
	    onSuccess : function(result) {
	    	if(result.buttonIndex == '0'){
	    		var index = $(obj).parent().parent().index();
	    		$(obj).parent().parent().remove();
	    		photoUrl.splice(index,1);
	    	}
	        
	    },
	    onFail : function(err) {}
	});
	 
}
//预览图片
function showAllPic(imgOne){
	dd.biz.util.previewImage({
		urls: photoUrl,//图片地址列表
		current: imgOne,//当前显示的图片链接
		onSuccess : function(result) {
			/**/
		},
		onFail : function(err) {}
	})
}
